package edu.ustb.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import edu.ustb.domain.User;
import edu.ustb.result.Result;
import edu.ustb.service.UserEmailXiumiService;
import edu.ustb.service.UserEmailXiumiServiceImp;
import edu.ustb.service.UserPhoneXiumiService;
import edu.ustb.service.UserPhoneXiumiServiceImp;

/**
 * Servlet implementation class UserEmaliXiumiController
 */
@WebServlet("/user/xiumiemail.do")
public class UserEmaliXiumiController extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String userEmail=request.getParameter("email");
		String userPhone=request.getParameter("phone");
		String yanzhengma=request.getParameter("yanzhengma");
		String isDo=request.getParameter("isDo");
		Result result=new Result();
		
		System.out.println(userEmail);
		UserEmailXiumiService useremailxiumi = new UserEmailXiumiServiceImp();
		
		User user = useremailxiumi.findByPhone(userPhone);
		System.out.println(user);
		if(userEmail.equals(user.getUserEmail())) {
			if("true".equals(isDo)) {
				String Yzm = useremailxiumi.creatYzm();
				int row = useremailxiumi.addYzmByEmail(Yzm,user.getUserEmail() );
				result.setCode(1);
				result.setMsg("邮箱验证码："+Yzm);
				response.getWriter().print(JSON.toJSONString(result));
				System.out.println(JSON.toJSONString(result));
				System.out.println("邮箱验证码："+Yzm);
			}else{
				if(yanzhengma.equals(user.getUserYzm()))
				{
					result.setCode(1);
					result.setMsg("success");
					response.getWriter().print(JSON.toJSONString(result));
					System.out.println(JSON.toJSONString(result));
				}else {
					result.setCode(2);
					result.setMsg("fail");
					response.getWriter().print(JSON.toJSONString(result));
					System.out.println(JSON.toJSONString(result));
				}
				
			}
		}else {
			result.setCode(3);
			result.setMsg("wrong Email");
			response.getWriter().print(JSON.toJSONString(result));
			System.out.println(JSON.toJSONString(result));
		}
		
		
		
			
	
	}
	

}
